package org.jeecg.modules.clue.service.impl;

import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.clue.entity.ClueRecordTask;
import org.jeecg.modules.clue.enums.CarPlate;
import org.jeecg.modules.clue.enums.TaskPushStatus;
import org.jeecg.modules.clue.factory.carPlate.CarPlateService;
import org.jeecg.modules.clue.mapper.ClueRecordTaskMapper;
import org.jeecg.modules.clue.req.ClueTaskChannelInviteStatusReq;
import org.jeecg.modules.clue.service.IClueRecordTaskService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 线索任务
 * @Author: jeecg-boot
 * @Date: 2025-01-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class ClueRecordTaskServiceImpl extends ServiceImpl<ClueRecordTaskMapper, ClueRecordTask> implements IClueRecordTaskService {


    @Resource
    Map<String, CarPlateService> carPlateServiceMap;
    @Resource(name = "clueMailAccount")
    MailAccount mailAccount;


    @Override

    public void pushTask(String taskId) {
        log.info("pushTask {}", taskId);
        ClueRecordTask recordTask = getById(taskId);
        if (null == recordTask) {
            return;
        }
        CarPlate carPlate = CarPlate.getInstance(recordTask.getTpCarConfigCode());
        CarPlateService carPlateService = carPlateServiceMap.get(carPlate.bean);
        carPlateService.push(recordTask);
        updateById(recordTask);
        String send = Mail.create(mailAccount)
                .setTos(mailAccount.getUser())
                .setTitle("线索推送任务结果")
                .setContent(JSON.toJSONString(recordTask))
                .send();
        log.info("邮件发送结果 {}", send);
    }

    @Override
    public void handlePush(String taskId) {
        log.info("handlePush {}", taskId);
        ClueRecordTask recordTask = getById(taskId);

        CarPlate carPlate = CarPlate.getInstance(recordTask.getTpCarConfigCode());
        CarPlateService carPlateService = carPlateServiceMap.get(carPlate.bean);
        carPlateService.push(recordTask);
        updateById(recordTask);

    }

    @Override
    public void channelInviteStatusUpdate(String id, String channelInviteStatus) {
        update(new LambdaUpdateWrapper<ClueRecordTask>()
                .eq(ClueRecordTask::getId, id)
                .set(ClueRecordTask::getChannelInviteStatus, channelInviteStatus)
                .set(ClueRecordTask::getChannelInviteTime,new Date())
        );
    }

    @Override
    public void channelInviteStatusSync(ClueTaskChannelInviteStatusReq clueTaskChannelInviteStatusReq) {
        update(new LambdaUpdateWrapper<ClueRecordTask>()
                .in(ClueRecordTask::getId, clueTaskChannelInviteStatusReq.getIds())
                .set(ClueRecordTask::getIzSync, clueTaskChannelInviteStatusReq.getStatus())
        );
    }


}
